/**
 * @author Krzysztof Rozalski
 */

package hapi.sprite.sheet;

import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.DisplayObjectContainer;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.display.Graphics;
import hapi.sprite.display.SpriteSheetDisplay;
import hapi.sprite.blit.BlitGraphicsSpriteSheetDisplay;
import hapi.sprite.display.ShapeSpriteSheetDisplay;

class BitmapFillSpriteSheet extends SpriteSheetBase, implements SpriteSheet {

	var matrix:Matrix;
	
	public function new(source, width:Int, height:Int, transparent=true, smoothing=false) {
		super(source, width, height);
		this.matrix = new Matrix(1, 0, 0, 1, 0, 0);
	}
	
	public inline function drawSpriteOn(canvas:Graphics, spriteId:Int, dest:Point):Void {
		updateCurrentSprite(spriteId);
		canvas.beginBitmapFill(source, matrix, false, false);
		canvas.drawRect(dest.x, dest.y, width, height); 
		canvas.endFill();
	}
	
	inline function updateCurrentSprite(spriteId:Int) {
		if(currentSpriteId != spriteId) {
			var row = Math.ceil(spriteId/cols);
			var col = spriteId - (cols * (row - 1));
			matrix.tx = (1 - col) * width;
			matrix.ty = (1 - row) * height;
			currentSpriteId = spriteId;
		}
	}
	
	public function createDisplay(?x:Float=0.0, ?y:Float=0.0):SpriteSheetDisplay {
		return createShapeDisplay(x, y);
	}
	
	public function createShapeDisplay(?x:Float = 0.0, ?y:Float = 0.0):ShapeSpriteSheetDisplay {
		return new ShapeSpriteSheetDisplay(this, x, y);
	}
	
	public function createBlitDisplay(?x:Float = 0.0, ?y:Float = 0.0):BlitGraphicsSpriteSheetDisplay {
		return new BlitGraphicsSpriteSheetDisplay(this, x, y);
	}
}